<template>
  <div>
    <div class="pageheader">
      <XHeader :left-options="{backText:''}" :title="xheader.title" class="XHeader"></XHeader>
    </div>
    <div class="pagetop">
      <div class="toptab">
        <div
          v-for="(item,index) in tab"
          :key="index"
          class="item"
          :class="tabIndex==index?'active':''"
          @click="onTabTap(index)"
        >{{item.text}}
        </div>
      </div>
    </div>
    <div class="page">
      <div>
        <div v-for="(item,index) in list" :key="index" class="block" style="margin-top: 12px;">
          <div
            class="getCouponItme"
            :class="item.state!=DISCOUNT_STATE['未使用']?'getCouponItmeEd':''"
          >
            <div
              class="image"
              :style="{backgroundImage: `url(${item.state!=DISCOUNT_STATE['未使用']?require('@/assets/img/discounted.png'):require('@/assets/img/discount.png')})`}"
            >
              <div class="money">{{item.reduce_amount}}</div>
              <div class="desc">满{{item.full_amount}}减{{item.reduce_amount}}</div>
            </div>
            <div class="content">
              <div class="dist">
                <div class="title">{{item.name}}</div>
                <div class="time">{{item.mobile_start_time}}-{{item.mobile_end_time}}</div>
              </div>
              <div v-if="item.state==DISCOUNT_STATE['未使用']" class="button" @click="onUseTap(item)">使用</div>
              <div v-if="item.state==DISCOUNT_STATE['已过期']" class="button">已过期</div>
              <div v-if="item.state==DISCOUNT_STATE['已使用']" class="button">已使用</div>
            </div>
          </div>
        </div>

      </div>
      <load-more
        v-if="!more&&!list.length"
        :show-loading="false"
        :tip="'暂无数据'"
        background-color="#fbf9fe"
      ></load-more>
    </div>
  </div>
</template>

<script>
  import Enum from '@/config/enum'
  import Me from '@/dll/me'
  import moment from 'moment'

  export default {
    data () {
      let tab = []
      for (var i in Enum.DISCOUNT_STATE) {
        tab.push({
          text: i,
          value: Enum.DISCOUNT_STATE[i]
        })
      }
      return {
        xheader: {
          title: '我的优惠券'
        },
        DISCOUNT_STATE: Enum.DISCOUNT_STATE,
        tab: tab,
        tabIndex: 0,
        more: true,
        loading: false,
        page: 1,
        list: []
      }
    },
    created () {
      this.init()
    },
    methods: {
      init () {
        this.more = true
        this.page = 1
        this.list = []
        this.getList()
      },
      onTabTap (index) {
        this.tabIndex = index
        this.init()
      },
      // 使用
      onUseTap (item) {
        let so = {
          coupon_id: item.coupon_id
        }
        this.$router.push({
          name: 'mallSearch',
          query: { so: JSON.stringify(so), title: '优惠券商品', load: true }
        })
      },
      // 加载列表
      async getList () {
        let state = this.tab[this.tabIndex].value
        this.$vux.loading.show({
          text: '加载中'
        })
        let res = await Me.getDiscountList(state)
        this.$vux.loading.hide()
        if (res.code == 0) {
          let list = res.data.map((item) => {
            let state
            if (item.is_used) {
              state = Enum.DISCOUNT_STATE.已使用
            } else {
              let day = moment()
              let end = moment(item.mobile_end_time).endOf('day')
              if (day.diff(end) >= 0) {
                state = Enum.DISCOUNT_STATE.已过期
              } else {
                state = Enum.DISCOUNT_STATE.未使用
              }
            }
            item.state = state
            return item
          })
          this.list = list
          this.more = false
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  .page {
    padding-top: 45px;
  }

  .pagetop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
  }

  .toptab {
    box-sizing: border-box;
    display: flex;
    padding-left: 12px;
    padding-right: 12px;
    height: 45px;
    background-color: #fff;
  }

  .toptab > .item {
    flex: 1;
    height: 100%;
    display: block;
    line-height: 45px;
    font-size: 14px;
    color: #351009;
    opacity: 0.2;
    text-align: center;
    position: relative;
  }

  .toptab > .item.active {
    opacity: 1;
  }

  .toptab > .item.active::after {
    position: absolute;
    content: "";
    width: 100%;
    bottom: 0;
    left: 0;
    height: 1px;
    background: #b0290f;
  }

  .getCouponItme {
    display: flex;
    flex-direction: row;
  }

  .getCouponItme .image {
    height: 88px;
    width: 120px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: url("../../../assets/img/discount.png");
  }

  .getCouponItme .image .money {
    font-size: 32px;
    color: #fff;
    line-height: 45px;
  }

  .getCouponItme .image .desc {
    font-size: 12px;
    color: #fff;
    line-height: 17px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }

  .getCouponItme .content {
    padding: 16px 16px 16px 24px;
    background-color: #fff;
    flex: 1;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    overflow: hidden;
  }

  .getCouponItme .content .dist {
    flex: 1;
    overflow: hidden;
  }

  .getCouponItme .content .dist .title {
    display: block;
    width: 100%;
    font-size: 18px;
    color: #351009;
    line-height: 25px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  }

  .getCouponItme .content .dist .time {
    font-size: 12px;
    color: #a4928f;
    line-height: 17px;
    margin-top: 13px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  }

  .getCouponItme .content .button {
    background-image: linear-gradient(135deg, #ff9d9d 0%, #c62c12 100%);
    text-align: center;
    border-radius: 12px;
    min-width: 50px;
    height: 24px;
    width: 50px;
    font-size: 12px;
    color: #fff;
    line-height: 24px;
    margin-left: 16px;
  }

  .getCouponItmeEd .image {
    background-image: url("../../../assets/img/discounted.png");
  }

  .getCouponItmeEd .content .button {
    background-image: linear-gradient(135deg, #999 0%, #666 100%);
  }
</style>
